package flex.cc.store.controller;

import java.io.File;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import flex.cc.PublicUtil;
import flex.cc.store.service.PackageExportService;
import flex.cc.util.FileDownloadUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import flex.cc.core.support.BaseController;
import flex.cc.store.dto.entity.StorePackageEntity;
import flex.cc.store.service.StorePackageService;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 * @author WANGCONG  update ByKSY
 * 2021年12月30日下午13:35:02
 * @desc 服务包管理：查询、导出
 */
@RestController
@RequestMapping(value = "/package")
public class PackageController extends BaseController{

	@Resource
	private StorePackageService packageService;
    @Resource
    private PackageExportService packageExportService;


	@PostMapping(value = "/queryListWithPage")
    @ApiOperation(httpMethod = "POST", value = "查询服务包列表")
    public Wrapper<IPage<StorePackageEntity>> queryOrderListWithPage(@ApiParam(name = "storePackage", value = "服务包信息") @RequestBody Map<String, Object> reqVo) {
        logger.info("查询服务包列表storePackage={}", reqVo);
        StorePackageEntity storePackageVo = initVo(reqVo);
        IPage<StorePackageEntity> page = packageService.queryPackageListWithPage(storePackageVo);
        return WrapMapper.ok(page);
    }

    /**
     * @desc 初始化构建查询VO对象
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public StorePackageEntity initVo(Map<String, Object> reqVo) {
        StorePackageEntity storePackageVo = new StorePackageEntity();
        storePackageVo.setPackageCodeCF( PublicUtil.isNotEmpty(reqVo.get("packageCodeCF")) ? reqVo.get("packageCodeCF").toString() : null );
        storePackageVo.setChannel( PublicUtil.isNotEmpty(reqVo.get("channel")) ? reqVo.get("channel").toString() : null );
        storePackageVo.setServer( PublicUtil.isNotEmpty(reqVo.get("server")) ? reqVo.get("server").toString() : null );
        String status = PublicUtil.isNotEmpty(reqVo.get("status")) ? reqVo.get("status").toString() : null;
        storePackageVo.setStartTime( PublicUtil.isNotEmpty(reqVo.get("startTime")) ? reqVo.get("startTime").toString() : null );
        storePackageVo.setEndTime( PublicUtil.isNotEmpty(reqVo.get("endTime")) ? reqVo.get("endTime").toString() : null );
        if(StringUtils.isNotEmpty(status)){
            storePackageVo.setStatusArr(status.split(","));
        }
        storePackageVo.setPageNum( Integer.parseInt(String.valueOf(reqVo.get("pageNum"))) );
        storePackageVo.setPageSize( Integer.parseInt(String.valueOf(reqVo.get("pageSize"))) );
        return storePackageVo;
    }

    @PostMapping(value = "/exportAll")
    @ApiOperation(httpMethod = "POST", value = "服务包全部导出")
    public void exportAll(HttpServletRequest request, HttpServletResponse response,
                          @ApiParam(name = "reqVo", value = "请求信息") @RequestBody Map<String, Object> reqVo){
        logger.info("服务包全部导出requestVo={}", reqVo);
        StorePackageEntity storePackageVo = initVo(reqVo);
        storePackageVo.setPageNum(1);
        if(Integer.parseInt(String.valueOf(reqVo.get("total")))== 0){
            storePackageVo.setPageSize( 999 );
        }else{
            storePackageVo.setPageSize( Integer.parseInt(String.valueOf(reqVo.get("total"))) );
        }
        String fileParentPath = System.getProperty("user.dir") + File.separator + "package" + File.separator + System.currentTimeMillis();
        String fileName = "服务包列表";
        packageExportService.downLoadSelection(request, response, storePackageVo, fileParentPath, fileName, "2");
        String filepath = fileParentPath + File.separator + fileName + ".xlsx";
        FileDownloadUtil.downloadByFilepath(request, response, filepath);
    }

    @PostMapping(value = "/exportSelections")
    @ApiOperation(httpMethod = "POST", value = "服务包部分导出")
    public void exportSelections(HttpServletRequest request, HttpServletResponse response, @RequestBody StorePackageEntity storePackageEntity){
        logger.info("服务包部分导出");
        String dataIds = storePackageEntity.getDataId();
        System.out.println(dataIds);
        if(StringUtils.isNotEmpty(dataIds)){
            storePackageEntity.setDataIdArr(dataIds.split(","));
            storePackageEntity.setPageNum(1);
            storePackageEntity.setPageSize(dataIds.split(",").length);
            String fileParentPath = System.getProperty("user.dir") + File.separator + "package" + File.separator + System.currentTimeMillis();
            String fileName = "服务包列表";
            packageExportService.downLoadSelection(request, response, storePackageEntity, fileParentPath, fileName, "1");
            String filepath = fileParentPath + File.separator + fileName + ".xlsx";
            FileDownloadUtil.downloadByFilepath(request, response, filepath);
        }
    }
}
